package Code.Objects.Effects.Projectiles;

import org.jrabbit.standard.game.graphics.skins.animation.AnimatedSkin;

import Code.Basic.Entity.GenericCharacter;
import Code.Basic.Entity.GenericEffect;

public class Bullet extends GenericEffect
{
	protected float range;
	protected float speed;
	protected float rangeCounter;

	protected float damage;
	protected int type;

	protected boolean hitTarget;

	public Bullet(String filepath, float r, double a, float s, int t, float d)
	{
		super(filepath);
		setUpBulletInfo(r, a, s, t, d);
	}

	public Bullet(AnimatedSkin skin, float r, double a, float s, int t, float d)
	{
		super(skin);
		setUpBulletInfo(r, a, s, t, d);
	}

	private void setUpBulletInfo(float r, double a, float s, int t, float d)
	{
		range = r;
		rotation.set(a);
		speed = s / 10000;
		damage = d;
		type = t;
		hitTarget = false;
	}

	public void update(int delta)
	{
		super.update(delta);
		if(rangeCounter >= range || hitTarget)
		{
			die();
		}
		else
		{
			double radians = (float) rotation.theta();
			location.add(Math.cos(radians) * speed * delta, Math.sin(radians) * speed * delta);
			rangeCounter += delta * speed;
		}
	}

	public void affect(GenericCharacter gC)
	{
		if(targetsAllowed[gC.getFaction()])
		{
			gC.damage(damage, type);
			hitTarget = true;
		}
	}

	protected void die()
	{
		getGameWorld().removeEffect(this);
	}
}
